/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

//import com.mysql.jdbc.Connection;
import dao.FtpInfoCrud;
import dao.ProviderDao;
import dao.ScheduleDataDao;
import java.sql.Connection;
import java.util.Vector;
import pojo.FtpInfo;
import pojo.Provider;
import pojo.Scheduledata;

/**
 *
 * @author A7med Kotb
 */
public class EditProvider {

    private Connection con;
    private FtpInfo ftpinfo;
    private Provider mypro;

    public EditProvider(Connection c) {
        try {
            con = c;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public Provider generalInfo(String providerName) {
        ProviderDao prodao = new ProviderDao(con);
        Provider mypro = prodao.getByName(providerName);
        return mypro;
    }

    public Provider getProviderById(int id) {
        ProviderDao prodao = new ProviderDao(con);
        Provider mypro = prodao.getById(id);
        return mypro;
    }

    public FtpInfo ftpinformaion(int providerid) {
        FtpInfoCrud ftpdao = new FtpInfoCrud(con);
        ftpinfo = ftpdao.selectById(providerid);
        return ftpinfo;
    }

    public Scheduledata scheduleInformation(int scid) {
        ScheduleDataDao scdao = new ScheduleDataDao(con);
        Scheduledata sc = scdao.getask(scid);
        return sc;
    }

    //this method to update provider general data
    public boolean updateGeneral(Provider pro) {
        ProviderDao prodao = new ProviderDao(con);
        Provider myold = prodao.getByName(pro.getName());
        pro.setPId(myold.getPId());
        pro.setHeaders(myold.getHeaders());
        return prodao.update(pro);
    }

    //method to update provider FTP data
    public boolean updateFtp(FtpInfo ftpinfo, Provider pro) {
        FtpInfoCrud ftpcrud = new FtpInfoCrud(con);
        ProviderDao prodao = new ProviderDao(con);
        Provider myold = prodao.getByName(pro.getName());
        return ftpcrud.update(ftpinfo, myold.getPId());
    }

    //method to update schedule data for certain provider
    public boolean updateschedule(Scheduledata scdata, Provider pro) {
        ScheduleDataDao scdao = new ScheduleDataDao(con);
        ProviderDao prodao = new ProviderDao(con);
        Provider myold = prodao.getByName(pro.getName());
        scdata.setScId(pro.getPId());
        return scdao.update(scdata);
    }

    //this method for updating schedule task data in database
    public boolean UpdateproviderSchedule(Scheduledata scdata) {
        ScheduleDataDao scdao = new ScheduleDataDao(con);
        boolean check = scdao.update(scdata);
        return check;
    }

    public Vector<Provider> getAll() {
        ProviderDao prodao = new ProviderDao(con);
        return prodao.getAll();
    }

    //this method to delete provider from database
    public boolean deleteProvider(String providerName, int id) {
        boolean check = false;
        ProviderDao pdao = new ProviderDao(con);
        check = pdao.deleteByName(providerName);

        String tableName = providerName + "_dim";
        check = pdao.dropProviderTable(tableName);
        return check;
    }
}
